package com.example.lt.config;

import com.example.lt.interceptor.JwtTokenInterceptor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author 小涛Tao
 * @Date: 2024/07/29/14:45
 * version 1.0 注释：
 **/
@Slf4j
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/admin/**","/authorize/**")
                .addPathPatterns("/joyfulMoments/**")
                .excludePathPatterns("/joyfulMoments/login/**","/joyfulMoments/index/**","/joyfulMoments/cdn/**","/joyfulMoments/file/**");

    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        String[] url = {"http://101.35.228.84:8880","http://101.35.228.84:5378","http://127.0.0.1:5378","http://localhost:5378"};

        registry.addMapping("/**")
                .allowedOrigins(url)
                .allowCredentials(true)
                .allowedMethods("*")   // 允许跨域的方法，可以单独配置
                .allowedHeaders("*");  // 允许跨域的请求头，可以单独配置;
    }

}
